$(document).ready(function(){
    $('#input').submit(function(){
        if ( $("#change").val() == "1")
            return true;
        
        var returnresult = true;
        $('#productInformation input[type=text]').each (function() {

           switch ($(this).attr("class")) {
               case 'reqstring niceinput':
                   result = verifystring ($(this).val(), "1");

                   if ($(this).parent().parent().children().length == 4 ){
                       $(this).parent().parent().children().eq(3).remove ();                              
                   }
                   if ( result != "1") {
                       $(this).parent().parent().append ( result );
                       returnresult = false;
                       }
                   break;
               case 'optdouble niceinput':                  
                   result = verifydouble ($(this).val(), "0");
                   if ( $(this).parent().parent().children().length == 4 )
                       $(this).parent().parent().children().eq(3).remove ();

                   if (  result != "1" ) {
                       $(this).parent().parent().append (result);
                       returnresult = false;
                       }
                   break;
               case 'optinteger niceinput':                 
                   result = verifyinteger ($(this).val(), "0");
                   if ($(this).parent().parent().children().length == 4 )
                       $(this).parent().parent().children().eq(3).remove ();

                   if (  result != "1" ){
                       $(this).parent().parent().append (result);                   
                       returnresult = false;
                       }
                   break;
               case 'reqdouble niceinput':                  
                   result = verifydouble ($(this).val(), "1");
                   if ( $(this).parent().parent().children().length == 4 )
                       $(this).parent().parent().children().eq(3).remove ();

                   if (  result != "1" ){
                       $(this).parent().parent().append (result);                   
                       returnresult = false;
                       }
                   break;
               case 'reqinteger niceinput':                  
                   result = verifyinteger ($(this).val(), "1");
                   if ($(this).parent().parent().children().length == 4 )
                       $(this).parent().parent().children().eq(3).remove ();

                   if (  result != "1" ){
                       $(this).parent().parent().append (result);                   
                       returnresult = false;
                       }
                   break;
               default:                  
                   result = verifystring ($(this).val(), "0");
                   if ($(this).parent().parent().children().length == 4 )
                       $(this).parent().parent().children().eq(3).remove ();

                   if (  result != "1" ){
                       $(this).parent().parent().append (result);                       
                       returnresult = false;
                       }
                   break;
           } 
        });
        
        return returnresult;
    });

    $("#categorypicker").change (function(){
        $('#change').val("1");
        $("#input").submit();
    });
    function required (str) {
        if ( str == null || str.length < 1 )
            return "1";
    }
    function verifystring (str, req) {
        if ( req == "1" && required(str)=="1")
             return "<td class=\"signup\">Warning: Valid string is required</td>";
        else
            return "1";
    }
    function verifyinteger (str, req) {
        if ( req == "1" && required(str)=="1")
            return "<td class=\"signup\">Warning: Integer value is required</td>";

        var reg = /^([\d-]{1,4})?$/;
        if ( !reg.test (str))
            return "<td class=\"signup\">Warning: Valid integer value is required</td>";
        else
            return "1";
    }
    function verifydouble (str, req) {
        if ( req == "1" && required(str)=="1")
            return "<td class=\"signup\">Warning: Double value is required</td>";

        var reg = /^([\d-]*(\.){0,1}[\d-]{0,4})?$/;
        if ( !reg.test (str))
            return "<td class=\"signup\">Warning: Valid double value is required</td>";
        else
            return "1";
    }
});